﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;

using Eforcity.Common35;
using Eforcity.Data.Common35;
using Eforcity.EbayApi;

namespace ConsoleGetOrdersEbay
{
    class Program
    {
        readonly static string _ErrorLogFilename = "GetSellerTransactionErr.txt";
        static void Main(string[] args)
        {
            Console.Title = "Get eBay Seller Transactions";

            while (true)
            {
                try
                {

                    List<int> sellerIdList = SellerIdList(AppSettings.EpacketDBConn);
                    foreach (int sellerId in sellerIdList)
                    {
                        try
                        {
                            ConsoleWrite("Get Sellerid:" + sellerId.ToString() + " Transaction Date Plase Wait.");
                            EbayTradingApi ebayApi = new EbayTradingApi(AppSettings.EbayApiEndPoint, AppSettings.EbayApiMaxRetry, AppSettings.EbaySessionCallLimit, sellerId, false, AppSettings.EbayMinCallAvail, AppSettings.EbayApiDbConn);
                            ebayApi.GetSellerTransactionsInfo(1, 100, AppSettings.ApiSleepTime, AppSettings.EpacketDBConn);
                        }
                        catch (Exception ex)
                        {
                            ConsoleWrite(ex.Message);
                        }
                        ConsoleWrite("Get Sellerid:" + sellerId.ToString() + " Transaction Date Is Finish！");
                    }


                    TransactionToCA(AppSettings.EpacketDBConn);
                    ConsoleSleep(AppSettings.IdleSleepTime);

                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.ToString());
                    FileUtility.AppendFile(_ErrorLogFilename, DateTime.Now.ToString() + FileUtility.NewLine + ex.ToString());
                    ConsoleSleep(AppSettings.IdleSleepTime);
                }
            }


        }

        private static List<int> SellerIdList(ConnectionStringSettings ePacketConn)
        {
            string funMsg = " private List<int> SellerIdList(ConnectionStringSettings Conn)";
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select distinct  Sellerid,ApiSelleruserid from dbo.EYBAccounts where state=0 order by SellerId asc");
            List<int> sellerList = new List<int>();
            try
            {
                DataTable table = DbUtility.GetDataTable(strSql.ToString(), ePacketConn);
                foreach (DataRow Row in table.Rows)
                {
                    sellerList.Add(ConvertUtility.ToInt(Row["Sellerid"]));
                }

                return sellerList;
            }
            catch (Exception ex)
            {
                string exMsg = "Exception:" + ex.ToString() + FileUtility.NewLine + "Sql:" + strSql.ToString() + FileUtility.NewLine + funMsg;
                throw new Exception(exMsg, ex);
            }
        }

        public static DataTable TransactionToCA(ConnectionStringSettings ePacketConn)
        {
            string funMsg = " public static DataTable TransactionToCA(ConnectionStringSettings ePacketConn)";

            StringBuilder strSql = new StringBuilder();
            strSql.AppendLine(" exec dbo.proc_TransactionToCA ");

            try
            {
                DataTable tabe = DbUtility.GetDataTable(strSql.ToString(), ePacketConn);

                return tabe;
            }
            catch (Exception ex)
            {
                string exMsg = "Exception:" + ex.ToString() + FileUtility.NewLine + "Sql:" + strSql.ToString() + FileUtility.NewLine + funMsg;
                throw new Exception(exMsg, ex);
            }
        }

        private static void ConsoleSleep(int sleepTime)
        {
            sleepTime = sleepTime <= 0 ? 10 : sleepTime;
            Console.WriteLine("System will sleep " + sleepTime + " mins...");
            System.Threading.Thread.Sleep(sleepTime * 60 * 1000);
        }

        private static void ConsoleWrite(string msg)
        {
            msg = string.Format("{0}\t{1}\r\n", DateTime.Now.ToString("yyyy-MM-dd HH:mm"), msg);
            Console.WriteLine(msg);
        }
    }
}
